package study.springboot.mybatis.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import study.springboot.mybatis.mapper.user.useradmin.UserAdminEO;
import study.springboot.mybatis.mapper.user.useradmin.UserAdminMapper;
import study.springboot.mybatis.mapper.user.userbase.UserBaseEO;
import study.springboot.mybatis.mapper.user.userbase.UserBaseMapper;

import java.util.List;

@Slf4j
@Service("userService")
public class UserService {

    @Autowired
    private UserBaseMapper userBaseMapper;
    @Autowired
    private UserAdminMapper userAdminMapper;

    public PageInfo<UserBaseEO> getUserBasePageInfoLt(int pageNo, int pageSize) {
        //必须在mapper接口中的方法执行之前设置该分页信息，要紧接着查询，否则可能不分页
        Page<UserBaseEO> page = PageHelper.startPage(pageNo, pageSize);
        List<UserBaseEO> ubEOLt = userBaseMapper.getUserBaseLt();
        PageInfo<UserBaseEO> pageInfo = new PageInfo(ubEOLt);
        return pageInfo;
    }

    public Page<UserBaseEO> getUserBasePageLt(int pageNo, int pageSize) {
        Page<UserBaseEO> page = userBaseMapper.getUserBasePageLt(pageNo, pageSize);
        return page;
    }

    @Transactional
    public void addAdmin(boolean isThrowEx) {
        UserBaseEO ubEO = new UserBaseEO();
        ubEO.setUbLoginName("wzj");
        ubEO.setUbLoginPwd("123123");
        userBaseMapper.insert(ubEO);
        if (isThrowEx) {
            throw new RuntimeException("我是一个异常");
        }
        UserAdminEO uaEO = new UserAdminEO();
        uaEO.setUaId(ubEO.getUbId());
        userAdminMapper.insert(uaEO);
    }
}
